﻿package cn.antscript.airCMS.data 
{
	import cn.antscript.airCMS.data.singleData.AbstractData;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	/**
	 * ...
	 * @author 
	 */
	public class XMLData extends EventDispatcher
	{
		private var xml:XML;
		private var xmlURL:String = "data/config.xml";
		
		
		public var siteName:String;
		
		public var pagesData:Vector.<PageData>;
		
		
		private static var _instance:XMLData;
		public function XMLData(privateClass:PrivateClass) 
		{
			
		}
		
		public static function getInstance():XMLData {
			
			if (XMLData._instance == null) {
				XMLData._instance = new XMLData(new PrivateClass);
			}
			
			return XMLData._instance;
		}
		
		public function loadXML():void {
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onLoadPathCom);
			loader.addEventListener(IOErrorEvent.IO_ERROR, onLoadErr);
			loader.load(new URLRequest(xmlURL));
		}
		
		private function onLoadPathCom(e:Event):void 
		{
			e.target.removeEventListener(Event.COMPLETE, onLoadPathCom);
			e.target.removeEventListener(IOErrorEvent.IO_ERROR, onLoadErr);
			
			
			xmlURL = String(XML(e.target.data).config.@webdataPath);
			Config.LOGIN_URL=String(XML(e.target.data).config.@loginURL);
			Config.SHARE_NAME = String(XML(e.target.data).config.@shareName);
			Config.IMG_UPLOAD_URL=String(XML(e.target.data).config.@imgUploadUrl);
			Config.FILE_UPLOAD_URL=String(XML(e.target.data).config.@fileUploadUrl);
			Config.UPDATE_URL=String(XML(e.target.data).config.@updateUrl);
			
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onLoadCom);
			loader.addEventListener(IOErrorEvent.IO_ERROR, onLoadErr);
			loader.load(new URLRequest(xmlURL));
		}
		
		private function onLoadErr(e:IOErrorEvent):void 
		{
			e.target.removeEventListener(Event.COMPLETE, onLoadPathCom);
			e.target.removeEventListener(Event.COMPLETE, onLoadCom);
			e.target.removeEventListener(IOErrorEvent.IO_ERROR, onLoadErr);			
			
			dispatchEvent(e);
		}
		
		private function onLoadCom(e:Event):void 
		{
			e.target.removeEventListener(Event.COMPLETE, onLoadCom);
			e.target.removeEventListener(IOErrorEvent.IO_ERROR, onLoadErr);
			
			xml = XML(e.target.data);
			
			siteName = String(xml.site.label);
			
			pagesData = new Vector.<PageData>();
			for (var i:int = 0; i < xml.site.page.length(); i++) 
			{
				pagesData.push(new PageData(XML(xml.site.page[i])));
			}		
			
			dispatchEvent(e);
		}
		
	}

}

class PrivateClass {
	
}